<template>
  <div class="todoapp">
    <TodoHeader @create="createFn"></TodoHeader>
    <TodoMain :arr="showArr" @del="deleteFn"></TodoMain>
    <TodoFooter
      :farr="showArr"
      @changeType="typeFn"
      @clear="clearFun"
    ></TodoFooter>
  </div>
</template>

<script>
import "./styles/base.css";
import "./styles/index.css";
//组件引入
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  //组件注册
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    //数据
    return {
      list: JSON.parse(localStorage.getItem("todoLIst")) || [],
      //中转字符串
      getSel: "all", //默认全部
    };
  },
  methods: {
    createFn(taskName) {
      //添加任务
      //push到数组
      let id =
        this.list.length == 0 ? 1 : this.list[this.list.length - 1].id + 1;
      this.list.push({
        id: id,
        name: taskName,
        isDone: false,
      });
    },
    deleteFn(index) {
      //删除
      this.list.splice(index, 1);
    },
    typeFn(str) {
      // all yes no
      this.getSel = str;
    },
    clearFun() {
      this.list = this.list.filter((obj) => obj.isDone == false);
    },
  },
  computed: {
    showArr() {
      if (this.getSel === "yes") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.getSel === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        //只要list变化 - 覆盖到localStorage里
        localStorage.setItem("todoList", JSON.stringify(this.list));
      },
    },
  },
};
</script>

<style>
</style>